package com.bjdd.jdy.ui.bankCard;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bjdd.jdy.R;
import com.bjdd.jdy.adapter.BankCardAdapter;
import com.bjdd.jdy.base.BaseActivity;
import com.bjdd.jdy.entity.BankCardBean;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.RecyclerViewDivider;
import com.bjdd.jdy.util.StatusBarUtil;
import com.bjdd.jdy.util.T;
import com.bjdd.jdy.view.other.DividerItemDecoration;
import com.yanzhenjie.recyclerview.swipe.Closeable;
import com.yanzhenjie.recyclerview.swipe.OnSwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BankCardActivity extends BaseActivity<BankCardPresenter, BankCardModel> implements BankCardContract.View {


    @Bind(R.id.toolbar_back)
    ImageView toolbarBack;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.rcv)
    SwipeMenuRecyclerView rcv;
    private List<BankCardBean> list;
    private BankCardAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_bank_card;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this,getResources().getColor(R.color.toolbar_bg),0);
        title.setText("我的银行卡");
        rcv.setLayoutManager(new LinearLayoutManager(context));
        //rcv.setItemViewSwipeEnabled(true);//开启滑动删除

        rcv.addItemDecoration(new DividerItemDecoration(context,DividerItemDecoration.VERTICAL_LIST));
        SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(context)
                        .setBackgroundColor(getResources().getColor(R.color.red))
                        .setText("删除")
                        .setTextColor(getResources().getColor(R.color.white))
                        .setWidth(100)
                        .setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
                swipeRightMenu.addMenuItem(deleteItem);
            }
        };
        rcv.setSwipeMenuCreator(swipeMenuCreator);
        rcv.setSwipeMenuItemClickListener(new OnSwipeMenuItemClickListener() {
            @Override
            public void onItemClick(Closeable closeable, int adapterPosition, int menuPosition, int direction) {
                if (direction == SwipeMenuRecyclerView.RIGHT_DIRECTION){
                    //右侧菜单
                    //T.show("删除："+menuPosition+"---"+adapterPosition);
                    if (menuPosition == 0){
                        presenter.delCard(list.get(adapterPosition).getCarrierCardId()+"",adapterPosition);
                    }
                }else if (direction == SwipeMenuRecyclerView.LEFT_DIRECTION){
                    //左侧菜单
                }
            }
        });
        list = new ArrayList<>();
        adapter = new BankCardAdapter(context,list);
        rcv.setAdapter(adapter);
    }


    @OnClick(R.id.toolbar_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void cardBack(List<BankCardBean> data) {
        if (data==null || data.size() <= 0){
            JumpUtil.toBindCard(context);
            finish();
        }else {
            list.clear();
            list.addAll(data);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void delCard(int position) {
        list.remove(position);
        adapter.notifyDataSetChanged();
        if (list.size() <= 0){
            JumpUtil.toBankCard(context);
            finish();
        }
    }
}
